package com.entor.util;

import java.beans.PropertyDescriptor;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtilsBean;

public class StringUtil {
	public static String getGradeCode(int year,int type){
		String gradeCode = "";
		String [] ym = GlobalPamaters.sdf_year_mm.format(new Date()).split("-");
		int y = Integer.parseInt(ym[0])-year;
		int m = Integer.parseInt(ym[1]);
		int s = 7;
		if(m>=9){
			y+=1;
		}else{
			y-=1;
		}
		s+=y;
		switch (type) {
			case 2:
				if(s==7){
					gradeCode = "21";
				}else if(s==8){
					gradeCode = "22";
				}else if(s==9){
					gradeCode = "23";
				}
				break;
			case 4:
				if(s==7){
					gradeCode = "31";
				}else if(s==8){
					gradeCode = "32";
				}else if(s==9){
					gradeCode = "33";
				}
				break;
			default:
				break;
		}
		return gradeCode;
	}
	
	public static Map<String, Object> beanToMap(Object obj) {
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			// 声明bean属性读取对象
			PropertyUtilsBean pub = new PropertyUtilsBean();
			// 把bean属性读取到一个descriptor数组中
			PropertyDescriptor[] pd = pub.getPropertyDescriptors(obj);
			// 循环通过属性名获取对应的值
			for (int i = 0; i < pd.length; i++) {
				// 获取属性名
				String name = pd[i].getName();
				// 排除构造函数
				if (!"class".equals(name)) {
					// 根据属性获取属性对应的值
					Object value = pub.getNestedProperty(obj, name);
					// 判断value不能空
					if (value != null && !"".equals(value)
							&& !"null".equals(value)) {
						// 添加到map
						params.put(name, value);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return params;
	}
	
	public static String checkSelcet(int type,String sanswer,String answer){
		String status = "2";
		switch (type) {
		case 1:
			if(sanswer.equals(answer)){
				status = "1";
			}
			break;
		case 2:
			char []c1 = sanswer.toCharArray();
			char []c2 = answer.toCharArray();
			if(c1.length!=c2.length){
				status = "2";
				break;
			}
			boolean b = false;
			for(char cc2 : c2){
				for(char cc1 : c1){
					if(cc1 == cc2){
						b = true;
						break;
					}else{
						b = false;
					}
				}
				if(b){
					status = "1";
				}else{
					break;
				}
			}
			
			break;	
		default:
			break;
		}
		return status;
	}

}
